$(document).ready(function () {
    $('title')[0].innerHTML='新增学校';

    $(".form_datetime").datetimepicker({format: 'yyyy-mm-dd',language: 'zh-CN',minView: "month", autoclose:true});
    $('[name=iMarketId]').select2();
    $('[name=iOperateId]').select2();
    $('#reset').click(function () {
        $('#form')[0].reset();
        // window.top.xhTop.getActiveFrame().location.reload();
        $('#provinceSelect').val(null).trigger("change");
        $('#citySelect').val(null).trigger("change");
        $('#countrySelect').val(null).trigger("change");
        myArea.loadArea({selectors:["provinceSelect","citySelect","countrySelect"]});
        $('#save').addClass('disabled');
        $('#form').initeValidate($('#save'));
    });
    $('#save').addClass('disabled');
    $('#form').initeValidate($('#save'));
    


    //绑定字典内容到指定的Select控件
    function BindSelect(ctrlName, url) {
        var control = $('#' + ctrlName);
        //设置Select2的处理
        control.select2({
            placeholder:'请选择',
            templateResult:  function formatRepo(repo){
                return repo.text
            },
            templateSelection: function (repo) {
                if(repo.id!=""){
                    $('#select2-'+ctrlName+'-container').parent().removeClass('warn-color').addClass('success-color');
                }
                return repo.text;
            },
            escapeMarkup: function (m) {
                $('#select2-'+ctrlName+'-container').parent().addClass('warn-color');
                return m;
            }
        });

        //绑定Ajax的内容
        $.getJSON(url, function (data) {
            control.empty();//清空下拉框
            control.append("<option value='' style='display: none;' selected>请选择</option>")
            $.each(data, function (id,item) {
                control.append("<option value='" + item['id'] + "'>" + item['text'] + "</option>");
            });
        });
    }

    BindSelect('provinceSelect',baseUrl+"customer/getAreaData?pid=0");
    BindSelect('citySelect',baseUrl+"customer/getAreaData?pid=1");
    BindSelect('countrySelect',baseUrl+"customer/getAreaData?pid=1");

    $("#provinceSelect").on("change", function (e) {
        var pid = $("#provinceSelect").val();
        $("#citySelect").val("").trigger('change');
        $("#countrySelect").val("").trigger('change');
        if(pid!=''){
            BindSelect("citySelect", baseUrl+"customer/getAreaData?pid="+pid);
        }
    });

    $("#citySelect").on("change", function (e) {
        var pid = $("#citySelect").val();
        $("#countrySelect").val("").trigger('change');
        if(pid!=''){
            BindSelect("countrySelect", baseUrl+"customer/getAreaData?pid="+pid);
        }
    });

    

    $('#save').click(function () {
        var commitData=$('#form').serialize();
        var str=decodeURI(commitData);
        // try{
            if($('#provinceSelect').select2('data')[0]==undefined){
                // alert('请选择省');
                window.top.swal({
                    title:'省未选择',
                    text:'请你选择省',
                    timer:700,
                    showConfirmButton:false,
                    
                });

                return false;
            }else if($('#citySelect').select2('data')[0]==undefined){
                // alert('请选择市');
                window.top.swal({
                    title:'市未选择',
                    text:'请选择市',
                    timer:700,
                    showConfirmButton:false,
                });
                return false;
            }else if($('#countrySelect').select2('data')[0]==undefined){
                // alert('请选择区');
                window.top.swal({
                    title:'区未选择',
                    text:'请选择区',
                    timer:700,
                    showConfirmButton:false,
                    
                });
                return false;
            }
            
            str= str.replace($('#provinceSelect').select2('data')[0].id,$('#provinceSelect ').select2('data')[0].text);
            str= str.replace($('#citySelect').select2('data')[0].id,$('#citySelect ').select2('data')[0].text);
            str=str.replace($('#countrySelect').select2('data')[0].id,$('#countrySelect ').select2('data')[0].text);

            str=encodeURI(str);
            $.ajax({
                url  :baseUrl+'customer/saveCustomerInfo',
                type : 'POST',
                data : str,
                dataType : 'JSON',
                success:function (data) {
                    window.top.xhTop.createNewTab("客户列表",baseUrl+'customer/customerList',true);
                    window.top.xhTop.closeTabByName('新增客户信息');
                },
                error:function (result) {
                    console.log(result);
                }

            });
    });

    $('#return').click(function () {
        try{
            if(window.top.xhTop.getFrameByName('客户列表')){
                window.top.xhTop.closeActiveTab();
            }else {
                window.top.xhTop.createNewTab("客户列表",baseUrl+'customer/customerList',false);
                window.top.xhTop.closeTabByName('新增客户信息');
            }
        }catch (e){
            window.open(baseUrl+'customer/customerList');
        }
        // window.open(baseUrl+'customer/customerList');
    });

});


